package main

import (
	"context"
	"fmt"
	"go_rpc/rpc/pb/test"
	"google.golang.org/grpc"
	"log"
	"net"
)

type GrpcServer struct {
	test.UnimplementedHelloServiceServer
}

func (g *GrpcServer) SayHello(ctx context.Context, req *test.HelloReq) (*test.HelloRep, error) {
	log.Printf("receviced grpc req: %+v", req.String())
	return &test.HelloRep{Msg: fmt.Sprintf("hello world ! %s", req.Name)}, nil
}

func main() {
	listen, err := net.Listen("tcp", ":8080")
	if err != nil {
		fmt.Println("异常:", err.Error())
		panic(err)
	}
	server := grpc.NewServer()
	test.RegisterHelloServiceServer(server, &GrpcServer{})
	err = server.Serve(listen)
	if err != nil {
		fmt.Println("异常:", err.Error())
		panic(err)
	}
}
